package com.wy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wy.entity.GoodsDeposit;
import com.wy.mapper.GoodsDepositMapper;
import com.wy.mapper.UserInfoMapper;
import com.wy.service.GoodsDepositService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author:王渝
 * @Date: 2022/11/30  11:16
 * @Version 1.0
 */
@Service
public class GoodsDepositServiceImpl extends ServiceImpl<GoodsDepositMapper, GoodsDeposit> implements GoodsDepositService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Transactional(rollbackFor = {})
    @Override
    public boolean payDeposit(GoodsDeposit goodsDeposit) {
        //扣取余额
        //update user_info set balance = balance - ? WHERE user_id = ? and balance - ? > 0
        int i = userInfoMapper.updateBalance(goodsDeposit.getAmount(), goodsDeposit.getUserId());
        if(i == 0){
            return false;
        }
        //添加到保证金表
        //INSERT INTO goods_deposit ( deposit_id, user_id, amount, create_time, update_time ) VALUES ( ?, ?, ?, ?, ? )
        boolean save = this.save(goodsDeposit);
        if(!save){
            //抛出异常 数据回滚
            throw new RuntimeException("保证金缴纳失败");
        }

        return true;
    }
}
